package com.ruoyi.project.para.paeaParaCalendar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.CommonParaTool;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.para.paeaParaCalendar.domain.PaeaParaCalendar;
import com.ruoyi.project.para.paeaParaCalendar.mapper.PaeaParaCalendarMapper;
import com.ruoyi.project.para.paeaParaCalendar.service.IPaeaParaCalendarService;
import com.ruoyi.project.tool.DateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * 工作日历Service业务层处理
 *
 * @author ruoyi
 * @date 2020-03-12
 */
@Service
public class PaeaParaCalendarServiceImpl extends ServiceImpl<PaeaParaCalendarMapper, PaeaParaCalendar> implements IPaeaParaCalendarService
{
    private static Logger logger = LoggerFactory.getLogger(PaeaParaCalendarServiceImpl.class);
    @Autowired
    private PaeaParaCalendarMapper paeaParaCalendarMapper;

    /**
     * 查询工作日历
     *
     * @param description 工作日历ID
     * @return 工作日历
     */
    @Override
    public PaeaParaCalendar selectPaeaParaCalendarById(String description)
    {
        return paeaParaCalendarMapper.selectPaeaParaCalendarById(description);
    }

    /**
     * 查询工作日历列表
     *
     * @param paeaParaCalendar 工作日历
     * @return 工作日历
     */
    @Override
    public List<PaeaParaCalendar> selectPaeaParaCalendarList(PaeaParaCalendar paeaParaCalendar)
    {
        return paeaParaCalendarMapper.selectPaeaParaCalendarList(paeaParaCalendar);
    }

    /**
     * 新增工作日历
     *
     * @param paeaParaCalendar 工作日历
     * @return 结果
     */
    @Override
    public int insertPaeaParaCalendar(PaeaParaCalendar paeaParaCalendar)
    {
        return paeaParaCalendarMapper.insertPaeaParaCalendar(paeaParaCalendar);
    }

    /**
     * 修改工作日历
     *
     * @param paeaParaCalendar 工作日历
     * @return 结果
     */
    @Override
    public int updatePaeaParaCalendar(PaeaParaCalendar paeaParaCalendar)
    {
        return paeaParaCalendarMapper.updatePaeaParaCalendar(paeaParaCalendar);
    }

    /**
     * 删除工作日历对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaParaCalendarByIds(String ids)
    {
        return paeaParaCalendarMapper.deletePaeaParaCalendarByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工作日历信息
     *
     * @param description 工作日历ID
     * @return 结果
     */
    @Override
    public int deletePaeaParaCalendarById(String description)
    {
        return paeaParaCalendarMapper.deletePaeaParaCalendarById(description);
    }

    @Override
    public List<PaeaParaCalendar> batchUpdatePaeaParaCalendar(String year, String dates) {
        int addCount =0;
        if (!StringUtils.isNull(year)) {
            this.remove(new QueryWrapper<PaeaParaCalendar>().eq("code",year));
        }
        List<PaeaParaCalendar> paeaParaCalendarList = Lists.newArrayList();
        if (!StringUtils.isEmpty(dates)) {
            Arrays.stream(Convert.toStrArray(dates)).forEach(date -> {
                PaeaParaCalendar paeaParaCalendar = new PaeaParaCalendar();
                paeaParaCalendar.setName("法定节假日");
                paeaParaCalendar.setType("public_holiday");
                paeaParaCalendar.setValue(date);
                paeaParaCalendar.setCode(year);
                paeaParaCalendarList.add(paeaParaCalendar);
            });
            this.saveBatch(paeaParaCalendarList);
            addCount = paeaParaCalendarList.size();
        }
        logger.info(String.format("更新工作日历成功：insert[%s]", addCount));
        return paeaParaCalendarList;
    }

    @Override
    public List<PaeaParaCalendar> initPaeaParaCalendar(String year) {
        int curYear = Integer.valueOf(year);
        Calendar calendarDate = Calendar.getInstance();
        calendarDate.set(Calendar.YEAR, curYear);
        calendarDate.set(Calendar.MONTH, 0);
        calendarDate.set(Calendar.DAY_OF_MONTH, 1);
        List<String> dateList = Lists.newArrayList();
        while (calendarDate.get(Calendar.YEAR) == curYear) {
            if (calendarDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                    || calendarDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                dateList.add(String.format("%s%02d%02d", curYear,(calendarDate.get(Calendar.MONTH) + 1),calendarDate.get(Calendar.DAY_OF_MONTH)));
            }
            calendarDate.add(Calendar.DATE, 1);
        }
        return batchUpdatePaeaParaCalendar(year,StringUtils.join(dateList,","));
    }

    /**
     * 取得日期+期限后的工作日
     * */
    public java.sql.Date findLimitDateWorkDay(int limit, java.sql.Date date){

        if(null != date){
            //计算期限天数内，有几个节假日
            int hoilNum = 0;
            for(int i=0; i<limit; i++){
                if(CommonParaTool.checkIsHoliday(DateTimeUtil.dayMouthAdd(date, i))){
                    hoilNum ++;
                }
            }

            String newDate = DateTimeUtil.dayMouthAdd(date, limit);
            // 是否节假日，true 是, false 否
            int d_num = 0;
            boolean flag = CommonParaTool.checkIsHoliday(newDate);
            while (flag) {
                d_num++;
                newDate = DateTimeUtil.dayMouthAdd(java.sql.Date.valueOf(newDate), d_num);
                flag = CommonParaTool.checkIsHoliday(newDate);
            }
            return this.getLastWorkDate(DateTimeUtil.dayMouthAdd(DateTimeUtil.parseSqlDate(newDate,DateTimeUtil.FMT_YYMMDD), hoilNum));

        }
        return new java.sql.Date(System.currentTimeMillis());
    }

    //得到最后的工作日
    public java.sql.Date getLastWorkDate(String date){

        int d_num = 0;
        String workDate = date;
        boolean flag = CommonParaTool.checkIsHoliday(workDate);
        while (flag) {
            d_num++;
            workDate = DateTimeUtil.dayMouthAdd(java.sql.Date.valueOf(workDate), d_num);
            flag = CommonParaTool.checkIsHoliday(workDate);
        }
        return java.sql.Date.valueOf(workDate);

    }
}
